﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Serial_assistant
{
    public partial class GPS : Form
    {
        private List<Double> longitude = new List<Double>();//数据结构----线性链表
        private List<Double> latitude = new List<Double>();//数据结构----线性链表
        public string jingdu,weidu;
        public string ewhemi = "E", nshemi = "N";           //南北纬，东西经
        public string altitude, speed;                //海拔高度，速度
        private Pen TablePen = new Pen(Color.Black, (float)2.5);//轴线颜色
        private Pen LinesPen = new Pen(Color.Red, (float)2.5);//波形颜色
        public ShowWindow ShowMainWindow;//定义显示主窗口委托访问权限为public
        public HideWindow HideMainWindow;//定义隐藏主窗口委托
        public OpenPort OpenSerialPort;//定义打开串口委托
        public ClosePort CloseSerialPort;//定义关闭串口委托
        public GetMainPos GetMainPos;//定义获取主窗口信息委托(自动对齐)
        public GetMainWidth GetMainWidth;//定义获取主窗口宽度(自动对齐)
        public GPS()
        {
            this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
                           ControlStyles.AllPaintingInWmPaint,
                           true);//开启双缓冲

            this.UpdateStyles();
            InitializeComponent();
        }

        private void GPS_Load(object sender, EventArgs e)
        {

        }


        private void Draw_mark(object sender, PaintEventArgs e)//画
        {
            System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
            e.Graphics.FillRectangle(Brushes.White, e.Graphics.ClipBounds);

            float a, b;

            if (longitude.Count - 1 >= 10000)//如果数据量大于可容纳的数据量，即删除最左数据
            {
                longitude.RemoveRange(0, 1);
                latitude.RemoveRange(0, 1);
            }

            e.Graphics.DrawLine(TablePen, new Point(0, 45), new Point(683, 45));
            e.Graphics.DrawLine(TablePen, new Point(45, 0), new Point(45, 683));
            e.Graphics.DrawLine(TablePen, new Point(0, 0), new Point(45, 45));
            e.Graphics.DrawLine(TablePen, new Point(680, 0), new Point(680, 680));
            e.Graphics.DrawString("/微度", this.Font,Brushes.Blue, new Point(12,0));
            e.Graphics.DrawString(ewhemi, this.Font, Brushes.Blue, new Point(30, 16));
            e.Graphics.DrawString(nshemi, this.Font, Brushes.Blue, new Point(5, 16));
            e.Graphics.DrawString("/微度", this.Font, Brushes.Blue, new Point(0,30));

            e.Graphics.DrawString("海拔高度: ", this.Font, Brushes.Black, new Point(700, 420));
            e.Graphics.DrawString("                ", this.Font, Brushes.Black, new Point(700, 440));
            e.Graphics.DrawString(altitude + " m", this.Font, Brushes.Black, new Point(700, 440));
            e.Graphics.DrawString("速度 : ", this.Font, Brushes.Black, new Point(700, 460));
            e.Graphics.DrawString("                ", this.Font, Brushes.Black, new Point(700, 480));
            e.Graphics.DrawString(speed + " km/h", this.Font, Brushes.Black, new Point(700, 480));
            e.Graphics.DrawString("纬度 : " , this.Font, Brushes.Black, new Point(700, 500));
            e.Graphics.DrawString("经度 : " , this.Font, Brushes.Black, new Point(700, 540));

            //Draw Y 纵向轴绘制
            for (int i = 0; i <= 10; i++)
            {
                e.Graphics.DrawString((i * 1000).ToString(),this.Font,Brushes.Black, new Point(50 + 60 * i, 15));
            }
            //Draw X 横向轴绘制
            for (int i = 0; i <= 10; i++)
            {
                e.Graphics.DrawString((i * 1000).ToString(), this.Font, Brushes.Black, new Point(15, 50 + 60 * i));
            }
            for (int i = 0; i < longitude.Count - 1; i++)//绘制
            {
                a = (float)((longitude[i] % 0.01) * 100000 * 0.6 + 50);
                b = (float)((latitude[i] % 0.01) * 100000 * 0.6 + 50);
                e.Graphics.FillEllipse(Brushes.Red, a, b,5,5);
            }
            e.Graphics.DrawString("                                                                              ", this.Font, Brushes.Black, new Point(700, 520));
            e.Graphics.DrawString(nshemi + " " + weidu + " 度", this.Font, Brushes.Black, new Point(700, 520));
            e.Graphics.DrawString("                                                                              ", this.Font, Brushes.Black, new Point(700, 560));
            e.Graphics.DrawString(ewhemi + " " + jingdu + " 度", this.Font, Brushes.Black, new Point(700, 560));
        }


        public void Add_longitude(Double Data)
        {
            longitude.Add(Data);//链表尾部添加数据
        }

        public void Add_latitude(Double Data)
        {
            latitude.Add(Data);//链表尾部添加数据
            Invalidate();//刷新显示
        }

        public void SetWindow(Point Pt)//允许主窗口设置自己
        {
            this.Location = Pt;
        }


        private void Drawer_FormClosing(object sender, FormClosingEventArgs e)
        {
            ShowMainWindow();//关闭自己，显示主窗口
        }

    }
}
